using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Serialcoder.Erp.Tools.PatchCreator
{
	public partial class PatchForm : Form
	{
		public PatchForm()
		{
			InitializeComponent();
		}

        private Runtime.Patch m_Patch;

        public Runtime.Patch Patch
        {
            get { return m_Patch; }
            set
            {                
                m_Patch = value; 
                bindingSource1.DataSource = m_Patch;

                uxTextEditorControl.Document.TextContent = m_Patch.Sql;

                if (m_Patch.Id != 0)
                {
                    LockUI();
                    
                }
            }
        }

        private void LockUI()
        {
            //this.Enabled = false;
            uxTextEditorControl.Document.ReadOnly = true;
            uxAuthorTextBox.ReadOnly = true;
            uxDescriptionTextBox.ReadOnly = true;

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            uxTextEditorControl.Document.DocumentChanged += new ICSharpCode.TextEditor.Document.DocumentEventHandler(Document_DocumentChanged);
            
            string appPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
            ICSharpCode.TextEditor.Document.HighlightingManager.Manager.AddSyntaxModeFileProvider(new ICSharpCode.TextEditor.Document.FileSyntaxModeProvider(appPath));

            uxTextEditorControl.Document.HighlightingStrategy = ICSharpCode.TextEditor.Document.HighlightingStrategyFactory.CreateHighlightingStrategy("SQL");
        }

        void Document_DocumentChanged(object sender, ICSharpCode.TextEditor.Document.DocumentEventArgs e)
        {
            if (m_Patch != null)
            {
                m_Patch.Sql = uxTextEditorControl.Document.TextContent;
            }
        }
        
        private void uxSaveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            

            if (string.IsNullOrEmpty(m_Patch.Description))
            {
                MessageBox.Show("description obligatoire");
                return;
            }

            if (string.IsNullOrEmpty(m_Patch.Author))
            {
                MessageBox.Show("auteur obligatoire");
                return;
            }

            if (string.IsNullOrEmpty(m_Patch.Sql))
            {
                MessageBox.Show("sql obligatoire");
                return;
            }

            try
            {
                Cursor.Current = Cursors.WaitCursor;
                Runtime.DAL.Save(m_Patch);
                LockUI();
            }
            catch (System.Data.SqlClient.SqlException ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }           
      
        }

        internal void InsertTemplate(string templateName)
        {
            uxTextEditorControl.Document.TextContent = Context.Current.Templates[templateName] + Environment.NewLine + uxTextEditorControl.Document.TextContent;
            //throw new Exception("The method or operation is not implemented.");
        }
    }
}